<template>
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="等级名" prop="levelName">
            <el-input v-model="form.levelName" placeholder="请输入等级名" />
        </el-form-item>

        <!-- <el-form-item label="删除标志" prop="del">
            <el-switch v-model="form.del" active-value="1" inactive-value="0" />
        </el-form-item> -->

        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { ref, defineProps } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const props = defineProps({
    form: {
        type: Object,
        default: () => {
            return {};
        }
    }
});

// 表单数据
const form = ref({
    levelName: '',
    del: 0
});
form.value = { ...props.form };

// 表单验证规则
const rules = {
    levelName: [{ required: true, message: '请输入等级名', trigger: 'blur' }],
    del: [{ required: true, message: '请选择删除标志', trigger: 'change' }]
};

const formRef = ref(null);

// 提交表单
const submitForm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            await axios.post("/api/consultantLevel/update", form.value);
            ElMessage.success('提交成功！');
            console.log('表单数据：', form.value);
        } else {
            ElMessage.error('请检查输入内容');
        }
    });
};

// 重置表单
const resetForm = () => {
    formRef.value.resetFields();
};
</script>

<style scoped>
.el-form {
    max-width: 500px;
    margin: 0 auto;
}
</style>
